package com.dsjwjj.wangjj.merchant.merchant;

import com.dsjwjj.wangjj.merchant.params.MerchantRequestParams;
import com.dsjwjj.wangjj.merchant.pojo.Merchant;
import com.dsjwjj.wangjj.merchant.service.MerchantService;
import com.dsjwjj.wangjj.utils.MD5;
import com.dsjwjj.wangjj.utils.R;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

@RestController
@RequestMapping("merchant")
public class MerchantHandler {
    @Resource
    private MerchantService merchantService;

    @RequestMapping("add")
    public R addMerchant(@RequestBody MerchantRequestParams merchant) {
        Merchant merchant_obj = merchantService.findMerchantByName(merchant.getMname());
        if (merchant_obj != null) {
            return R.fail("您注册的用户名已存在");

        }
        return merchantService.addMerchant(merchant) > 0 ? R.ok("注册成功") : R.fail("注册失败");
    }
    @RequestMapping("login")
    public R login(@RequestBody MerchantRequestParams merchant,HttpServletRequest request) {
//        merchant.setMpassword(MD5.getMD5Hash(merchant.getMpassword()));
        Merchant merchant_obj = merchantService.login(merchant);
        if (merchant_obj != null) {
            request.getSession().setAttribute("mname",merchant_obj.getMname());
        }
        return merchant_obj != null ? R.ok("登录成功") : R.fail("账号或密码错误");
    }
    @RequestMapping("update")
    public R updataMerchant(@RequestBody Merchant merchant,HttpServletRequest request){
       merchant.setMstatus(0);

        merchant.setMname(request.getSession().getAttribute("mname").toString());
        merchant.setUpdate_by(request.getSession().getAttribute("mname").toString());

        return merchantService.updateMerchant(merchant) >0? R.ok("资料提交成功，请等待审核"):R.fail("资料提交失败，请审核");
        // 好了，不在了嘛？
    }

}